var app = getApp()
var util = require("../../utils/utils.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isOver: false,
    activityList: [],
    page: 1,
    rows: 8
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: '活动列表'
    })
    this.queryActivityList();
  },
  queryChildActList: function() {
    let that = this;
    wx.request({
      url: app.httpUrl + '/activity/queryChildActList',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function(res) {
        var list = res.data.data;
        for (var i in list) {
          list[i].startD = util.formatDate(list[i].startDate, 'Y/M/D');
          list[i].endD = util.formatDate(list[i].endDate, 'M/D');
        }
        that.setData({
          childActList: list
        })
      }
    })
  },
  queryActivityList: function() {
    let that = this;
    wx.request({
      url: app.httpUrl + '/activity/queryActList',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      data: {
        page: that.data.page,
        rows: 10
      },
      success: function(res) {
        if (that.data.page >= res.data.data.totalPage) {
          that.setData({
            isOver: true,
            successSearchLoadingComplete: true
          })
        }
        console.log(res.data.data.results)
        that.data.page += 1; //请求完后页数+1
        let searchList = [];
        that.data.actList ? searchList = that.data.actList.concat(res.data.data.results) : searchList = res.data.data.results//判断data里面的数组是否为空 
        for (var i in searchList) {
          searchList[i].startD = util.formatDate(searchList[i].startDate, 'Y/M/D');
          searchList[i].endD = util.formatDate(searchList[i].endDate, 'M/D');
        }
        that.setData({
          actList: searchList
        }, () => {
          wx.hideLoading()
        })
      }
    })
  },
  activityLive: function(event) {
    var activityId = event.currentTarget.dataset.idx;
    var childId = event.currentTarget.dataset.childid;
    app.globalData.activityId = activityId;
    app.globalData.childId = childId;
    wx.switchTab({
      url: '/pages/study/study'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    wx.showLoading({
      title: '加载中....',
      mask: true
    })
    if (!this.data.isOver) {
      this.queryActivityList();
    } else {
      wx.showToast({
        title: '没有更多...',
        icon: 'none'
      })
      wx.hideLoading()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})